package com.fjut.kms.dao;

import com.fjut.kms.domain.Department;
import com.fjut.kms.tools.DBTools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Empathy on 2017/8/14.
 */
public class DepartmentJDBCImpl implements DepartmentDao {
    private static final String SQL="select deptName from departments";
    public List<Department> getDepartmentList(){
        Connection conn= DBTools.getConn();
        PreparedStatement pstmt=null;
        ResultSet rset=null;
        List<Department> deptList=new ArrayList<Department>();
        try{
            pstmt=conn.prepareStatement(SQL);
            rset=pstmt.executeQuery();
            while (rset.next()){
                Department dept=new Department();
                dept.setDeptName(rset.getString("deptName"));
                deptList.add(dept);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBTools.releaseRes(conn,pstmt,null);
        }
        return deptList.size()>0?deptList:null;
    }
}
